const debounce = (fn, wait) => {
    let t = null; //原理是闭包,使用闭包，缓存变量
    return function() {
        // let that = this
        if (t) {
            clearTimeout(t);
        }
        t = setTimeout(() => {
            fn.apply(this, arguments); //apply第二个参数是数组，call的多个参数用逗号隔开
            t = null;
        }, wait);
    };
};

const throttle = (fn, wait) => { //节流
    var prev = null; //原理是闭包,使用闭包，缓存变量
    // console.log('prev1', prev);在这打印不会运行
    return function() {
        var now = Date.now();
        // console.log('now', now);
        // console.log(now - prev > wait)
        if (now - prev > wait) {
            fn.apply(this, arguments)
            prev = Date.now()
                // console.log('prev2', prev);
        }
    }
};
const throttle1 = (fn, wait) => {
    let last = null;
    return function() {
        // let that = this
        let now = +new Date()
        if (now - last > wait || !last) {
            fn.apply(this, arguments);
            last = now
        }

    };
};

module.exports = {
    debounce,
    throttle,
};